﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.Framework.Client.Utils;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;
using DevExpress.XtraTreeList.Nodes;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class frmServiceItemList : Form
    {
        private DataSet dsServiceItem;
        private DataRow drSelected;
        public frmServiceItemList()
        {
            InitializeComponent();
        }

        public DataRow SelectedRow
        {
            get { return drSelected; }
        }

        public frmServiceItemList(DataSet ds): this()
        {
            dsServiceItem = ds.Copy();

            string stateFieldName = "STATENAME";
            if (dsServiceItem.Tables[0].Columns.IndexOf(stateFieldName) < 0)
            {
                DataColumn dc = new DataColumn(stateFieldName);
                dsServiceItem.Tables[0].Columns.Add(dc);
            }
            foreach (DataRow dr in dsServiceItem.Tables[0].Rows)
            {
                string sState = dr[DBEntityServiceItem.FieldName.STATE].ToString();
                if (sState == DBEntityServiceItem.ENUM_STATE.C0A)
                    dr[stateFieldName] = DBEntityServiceItem.ENUM_STATE.C0A_CHINESE;
                else if (sState == DBEntityServiceItem.ENUM_STATE.C0S)
                    dr[stateFieldName] = DBEntityServiceItem.ENUM_STATE.C0S_CHINESE;
            }
            dsServiceItem.AcceptChanges();

            gdServiceItem.DataSource = null;
            gdServiceItem.DataSource = dsServiceItem.Tables[0].DefaultView;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            int iRowHandle = gvServiceItem.FocusedRowHandle;
            if (iRowHandle < 0)
            {
                MsgBox.MsgInfo("请先选择服务项目。");
                return;
            }

            drSelected = gvServiceItem.GetDataRow(iRowHandle);
            this.DialogResult = DialogResult.OK;
        }

        private void gvServiceItem_DoubleClick(object sender, EventArgs e)
        {
            btnOK_Click(null, null);
        }

        private void frmServiceItemList_Shown(object sender, EventArgs e)
        {
            InitServiceItemCategory();
        }

        private void InitServiceItemCategory()
        {
            string error = string.Empty;
            DataSet dsServiceItemCategory = new DataSet();
            BussinessProxyManager.Instance.QueryServiceItemCategory(string.Empty, out dsServiceItemCategory, out error);
            DataRow[] drFinds = dsServiceItemCategory.Tables[0].Select("SCATEGORYID=0");
            if (drFinds.Length <= 0)
            {
                DataRow drNew = dsServiceItemCategory.Tables[0].NewRow();
                drNew[DBEntityServiceItemCategory.FieldName.SCATEGORYID] = 0;
                drNew[DBEntityServiceItemCategory.FieldName.SNAME] = "所有";
                drNew[DBEntityServiceItemCategory.FieldName.SPARENTCATEGORYID] = -1;
                dsServiceItemCategory.Tables[0].Rows.InsertAt(drNew, 0);
            }
            dsServiceItemCategory.AcceptChanges();

            tlCategory.KeyFieldName = DBEntityServiceItemCategory.FieldName.SCATEGORYID;
            tlCategory.ParentFieldName = DBEntityServiceItemCategory.FieldName.SPARENTCATEGORYID;
            tlCategory.DataSource = dsServiceItemCategory.Tables[0].DefaultView;

            ExpandNodes();
            TreeListNode fNode = tlCategory.FocusedNode;
            if (fNode != null)
            {
                tlCategory.FocusedNode = fNode;
            }
        }

        public void ExpandNodes()
        {
            if (tlCategory.Nodes.Count > 0)
            {
                tlCategory.Nodes[0].Expanded = true;
                foreach (TreeListNode node in tlCategory.Nodes[0].Nodes)
                {
                    node.Expanded = true;
                }
            }
        }

        private void tlCategory_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            DataRowView drv = (DataRowView)tlCategory.GetDataRecordByNode(tlCategory.FocusedNode);
            if (drv == null)
                return;

            int iCategoryID = Convert.ToInt32(drv[DBEntityServiceItemCategory.FieldName.SCATEGORYID]);
            if (gvServiceItem.DataSource != null)
            {
                DataView dv = gvServiceItem.DataSource as DataView;
                if (iCategoryID == 0)
                {
                    dv.RowFilter = "";
                }
                else
                {
                    dv.RowFilter = string.Format("{0}={1}", DBEntityServiceItem.FieldName.SCATEGORYID, iCategoryID.ToString());
                }
            }
        }
    }
}
